<?php
class sscls_User
{

    function login($username,$password)
    {
        $db=ss_Main::getDBO();
        $msg=Resources::_('LOGINSUCCESS');
        $suc=true;
        $error=array();
        $error['msg']=$msg;
        $error['suc']=$suc;
        if($username==''||$username==null)
        {
            $error['msg']=Resources::_('USERNAMEINPUT');
            $error['suc']=false;
            return $object; 
        }
        if($password==''||$password==null)
        {
            $error['msg']=Resources::_('PASSWORDINPUT');
            $error['suc']=false;
            return $error; 
        }
        
        $sql = "select * from Gebruiker where username = ".$db->QuoteSmart($username)." and password = '".md5($password)."'";
    
        //$rows=$db->loadList($sql);
        $row=$db->query($sql,true);  
        if(!$row)
        {           
            $error['msg']=Resources::_('LOGINNOTDONE');
            $error['suc']=false;
            return $error;             
        }
        
            
        $session=ss_Main::getSession();
        ini_set('session.cookie_lifetime',1000);
        $session->set('user',$row);
        
        return $error;
    }
    
    
    function logout()
    {
        $session=ss_Main::getSession();
        $session->set('user',NULL);
        return true;
    }
    //check status login
    function checklogin()
    {
        $session=ss_Main::getSession();
        $se=$session->get('user');
        if((int)$se->id!=0)
        {
            return true;
        }
        return false;
    }
    //get user current 
    function get() 
    {
        $session=ss_Main::getSession();
        $se=$session->get('user');
			if((int)$se->id!=0)
			{
				return $se;
			}
			return false;
      
    }
    
    
    function save($data)
    {
        global $main;
        $conf=$main->getConfig();
        $object=$main->get('object');
        $msg=Resources::_('SAVEDUSER');
        $object->add('msg',$msg);
        $object->add('suc',true);
        if((int)$data['id']!=0)
        {
            $uservalid=$this->checkvalid1($data);    
        }else
        {
            $uservalid=$this->checkvalid($data);
        }
        
        if(!$uservalid->suc)
        {
             $object->msg=$uservalid->msg;
             $object->suc=false;
             return $object; 
        }
        if(!$main->save($data,'users'))
        {
            
            $object->msg=Resources::_('ERRORSAVEUSER');;
            $object->suc=false;
            return $object;  
                        /**
             * if((int)$data['id']==0)
             *             {
             *                 $sendmail=$this->SendMailActive($data);
             *                 if(!$sendmail->suc)
             *                 {
             *                     $object->msg=$sendmail->msg;
             *                     $object->suc=false;
             *                     return $object;  
             *                 }
             *             }
             */
        }
        
        
        return $object; 
    }
    
    function checkvalid($data)
    {
        global $main;
        
        $object=$main->get('object');
        $msg=Resources::_('SAVEDUSER');
        $object->add('msg',$msg);
        $object->add('suc',true);

        //ho ten ko dc nho hon 4 ky tu
        if(strlen($data['fullname'])<=3)
        {
            if(strlen($data['fullname'])==0)
            {
                $msg=Resources::_('INPUTNAME');
            }else
                $msg=Resources::_('LENGNAME');
                
             $object->msg=$msg;
             $object->suc=false;
             return $object; 
        }
        //kiem tra email
        if(!$this->checkemailvalid($data['email']))
        {
            if(strlen($data['email'])==0)
            {
                $msg=Resources::_('INPUTEMAIL');
            }else
                $msg=Resources::_('EMAILVALID');
            
             $object->msg=$msg;
             $object->suc=false;
             return $object; 
        }
        //kiem tra su doc nhat cua email
        if(!$main->valueexists('users','email',$data['email']))
        {
            $msg=Resources::_('EMAIEXISTS');
            $object->msg=$msg;
            $object->suc=false;
            return $object;
        }
        //sdt ko dc nho hon 6 ky tu
        if(strlen($data['phone'])>0)
        {
            if(!is_numeric($data['phone']))
            {
                $msg=Resources::_('MOBILEVALD');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
            
            if(strlen($data['phone'])>11)
            {
                $msg=Resources::_('MOBILMAXLENGTH');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
                
        }
        
        if(strlen($data['cmnd'])>0)
        {
            if(!is_numeric($data['cmnd']))
            {
                $msg=Resources::_('CMNDVALID');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
            
            if(strlen($data['cmnd'])!=9)
            {
                $msg=Resources::_('CMND9');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
                
        }
        else
        {
            $msg=Resources::_('CMNDINPUT');
            $object->msg=$msg;
            $object->suc=false;
            return $object;
        }
        //kiem tra doc nhat cua cmnd
        if(!$main->valueexists('users','cmnd',$data['cmnd']))
        {
            $msg=Resources::_('CMNDEXISTS');
            $object->msg=$msg;
            $object->suc=false;
            return $object;
        }
        
        if(strlen(Request::getRequest('password'))<1)
            {
                $msg=Resources::_('PASSWORDINPUT');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
        
        if(Request::getRequest('password')!=Request::getRequest('repassword'))
        {
            $msg=Resources::_('PASSWORDNOTMATH');
            $object->msg=$msg;
            $object->suc=false;
            return $object;
        }
        if(Request::getRequest('captcha_user')=='')
        {
            $msg=Resources::_('CAPTCHAINPUT');
           $object->msg=$msg;
           $object->suc=false;
           return $object;
        }
        
        if(!$main->checkcaptcha('user_new1',Request::getRequest('captcha_user')))
        {
           $msg=Resources::_('CAPTCHANOTMATH');
           $object->msg=$msg;
           $object->suc=false;
           return $object; 
        }
    
        return $object;
    }
     function checkvalid1($data)
    {
        global $main,$user;
        
        $object=$main->get('object');
        $msg=Resources::_('SAVEDUSER');
        $object->add('msg',$msg);
        $object->add('suc',true);

        //ho ten ko dc nho hon 4 ky tu
        if(strlen($data['fullname'])<=3)
        {
            if(strlen($data['fullname'])==0)
            {
                $msg=Resources::_('INPUTNAME');
            }else
                $msg=Resources::_('LENGNAME');
                
             $object->msg=$msg;
             $object->suc=false;
             return $object; 
        }
        //sdt ko dc nho hon 6 ky tu
        if(strlen($data['phone'])>0)
        {
            if(!is_numeric($data['phone']))
            {
                $msg=Resources::_('MOBILEVALD');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
                
        }
        
        if(strlen(Request::getRequest('password'))>0)
            {
                if(Request::getRequest('oldpassword'))
                {
                    $passwordold=$user->get($data['id'])->password;
                    if($passwordold!=md5(Request::getRequest('oldpassword')))
                    {
                        $msg=Resources::_('OLDPASSWORDNOTMATCH');
                        $object->msg=$msg;
                        $object->suc=false;
                        return $object;
                    }    
                }
                
                if(Request::getRequest('password')!=Request::getRequest('repassword'))
                {
                    $msg=Resources::_('PASSWORDNOTMATH');
                    $object->msg=$msg;
                    $object->suc=false;
                    return $object;
                }
            }
        if(Request::getRequest('type')!='admin')
        {
            if(!$main->checkcaptcha('user_edit',Request::getRequest('captcha')))
            {
               $msg=Resources::_('CAPTCHANOTMATH');
               $object->msg=$msg;
               $object->suc=false;
               return $object; 
            }
        
        }
        
        return $object;
    }
    //Kiem tra tinh hop le cua email
    function checkemailvalid($mailStr)
    {
        $pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
        if(preg_match($pattern, $mailStr)==0) {
            return false;
        }
        else
            return true;
    }
    
    function SendMailActive($userinfo)
    {
        global $main,$user,$db,$config;
        $conf=$main->getConfig();
        $object=$main->get('object');
        $msg=Resources::_('SENDMAIL');
        $object->add('msg',$msg);
        $object->add('suc',true);

        //$content=$main->loadbyid('content',(int)$conf['sendmailactive']);
        $content=$db->loadObject('select * from content where id='.(int)$conf['sendmailactive']);
        
        $subject=Resources::_('ACCOUNTDETAIL');
        $message=Resources::lang($content->fulltext);
        //$urlactive='<a href="'.$config->live_site.'index.php?com=user&t=active&hashcode='.$userinfo['hashcode'].'" >'.$config->live_site.'index.php?com=user&t=active&hashcode='.$userinfo['hashcode'].'</a>';
       $urlactive= $userinfo['hashcode'];
        $message = str_replace("[[[link]]]", $urlactive, $message);
        $message = str_replace("[[[name]]]", $userinfo['fullname'], $message);
        $message = str_replace("[[[email]]]", $userinfo['email'], $message);
        $mail=$main->sendmailsmtp($userinfo['email'],$userinfo['fullname'],'','',$subject,$message);
        if(!$mail->suc)
        {
            $object->msg='OK';
            $object->suc=false;
            return $object;
        } 
        return $object;
    }
    //kich hoat tai khoan
    function active($hashcode=''){
        global $db,$main;
        $session=$main->getSession();
        $object=$main->get('object');
         $msg=Resources::_('Tài khoản của bạn đã được kích hoạt.');
        $object->add('msg',$msg);
        $object->add('suc',true);
        
        $query='select * from users where hashcode="'.$hashcode.'"';
        $row=$db->query($query,true);
        if($row['id']!=0)
        {
            if((int)$row['activated']==1)
            {
				
                $msg=Resources::_('ACTIVATEDERROR');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
		    $this->login($row['email'],$row['password']);
            //$userse=$session->get('user');
            $data['id']=$row['id'];
            $data['sessionid']=$session->getSessionID();
            $data['activated']=1;
            if(!$main->save($data,'users'))
            {
                $msg=Resources::_('ACTIVATEDERROR_');
                $object->msg=$msg;
                $object->suc=false;
                return $object;
            }
            return $object;             
        }
        else
        {
            $msg=Resources::_('DONTEXISTSHASECODE');
            $object->msg=$msg;
            $object->suc=false;
            return $object;
        }
            
    }
    
    function resetpass($email='')
    {
        global $main,$db;
        $conf=$main->getConfig();
        $object=$main->get('object');
        $msg=Resources::_('RETETPASSSSUC');
        $object->add('msg',$msg);
        $object->add('suc',true);
        $content=$db->loadObject('select * from content where id='.(int)$conf['resetpassword']);
        $row=$db->loadObject('select * from users where email=\''.$email.'\'');
        if($email=='')
        {
            $object->msg=Resources::_('INPUTEMAIL');;
            $object->suc=false;
            return $object;
        }
        if((int)$row->id==0)
        {
            $object->msg=Resources::_('DONTEXISTSEMAIL');;
            $object->suc=false;
            return $object;
        }
        
        if(!$main->checkcaptcha('user_resetpass',Request::getRequest('captcha_resetpass')))
        {
           $msg=Resources::_('CAPTCHANOTMATH');
           $object->msg=$msg;
           $object->suc=false;
           return $object; 
        }
        $passnew=$main->getRString(6);
        $newpass=md5($passnew);
        $data['id']=$row->id;
        $data['password']=$newpass;
        $main->save($data,'users');
        $subject='Mat khau moi cua ban tai dien dan  Troi oi bong da';
        $message=Resources::lang($content->fulltext);
        $message = str_replace("[[[newpass]]]", $passnew, $message);
        $message = str_replace("[[[name]]]", $row->fullname, $message);      
        $mail=$main->sendmailsmtp($row->email,$row->fullname,'','',$subject,$message);
        if(!$mail->suc)
        {
            $object->msg=$mail->msg;
            $object->suc=false;
            return $object;
        } 
        return $object;
    }
}
?>
